package com.yxx.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yxx.business.entity.*;
import com.yxx.business.service.PhaseService;
import com.yxx.common.utils.DateUtils;
import com.yxx.common.core.domain.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.yxx.common.core.service.impl.ServiceImplPlus;
import com.yxx.business.mapper.TbConstructionPhaseMapper;
import com.yxx.business.service.ITbConstructionPhaseService;

/**
 * 施工阶段Service业务层处理
 *
 * @author yxx
 * @date 2025-08-16
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy, @Autowired})
public class TbConstructionPhaseServiceImpl extends ServiceImplPlus<TbConstructionPhaseMapper, TbConstructionPhaseDo> implements ITbConstructionPhaseService, PhaseService {

    private final TbConstructionPhaseMapper tbConstructionPhaseMapper;

    /**
     * 查询施工阶段分页结果
     *
     * @param tbConstructionPhaseQueryDto 施工阶段查询实体
     * @return 施工阶段分页
     */
    @Override
    public PageResult<TbConstructionPhaseVo> selectTbConstructionPhaseVoPage(TbConstructionPhaseQueryDto tbConstructionPhaseQueryDto) {
        startPage();
        PageResult<TbConstructionPhaseVo> page
                = super.getMyBatisPageResult(selectTbConstructionPhaseDoList(tbConstructionPhaseQueryDto), TbConstructionPhaseVo.class);
        clearPage();
        return page;
    }

    /**
     * 查询施工阶段Vo列表
     *
     * @param tbConstructionPhaseQueryDto 施工阶段查询实体
     * @return 施工阶段集合
     */
    @Override
    public List<TbConstructionPhaseVo> selectTbConstructionPhaseVoList(TbConstructionPhaseQueryDto tbConstructionPhaseQueryDto) {
        return super.convertList(tbConstructionPhaseMapper.selectTbConstructionPhaseList(tbConstructionPhaseQueryDto), TbConstructionPhaseVo.class);
    }

    /**
     * 查询施工阶段Do列表
     *
     * @param tbConstructionPhaseQueryDto 施工阶段查询实体
     * @return 施工阶段集合
     */
    @Override
    public List<TbConstructionPhaseDo> selectTbConstructionPhaseDoList(TbConstructionPhaseQueryDto tbConstructionPhaseQueryDto) {
        return tbConstructionPhaseMapper.selectTbConstructionPhaseList(tbConstructionPhaseQueryDto);
    }

    /**
     * 查询单个施工阶段
     *
     * @param constructionId 施工阶段主键
     * @return 施工阶段单个
     */
    @Override
    public TbConstructionPhaseVo selectTbConstructionPhaseVoByConstructionId(Long constructionId) {
        return super.convertBean(tbConstructionPhaseMapper.selectTbConstructionPhaseByConstructionId(constructionId), TbConstructionPhaseVo.class);
    }

    /**
     * 新增施工阶段
     *
     * @param tbConstructionPhaseQueryDto 施工阶段编辑实体
     * @return 结果
     */
    @Override
    public int insertTbConstructionPhase(TbConstructionPhaseEditDto tbConstructionPhaseQueryDto) {
        tbConstructionPhaseQueryDto.setCreateById(getUserIdOrNotLogged());
        tbConstructionPhaseQueryDto.setCreateBy(getUserNameOrNotLogged());
        tbConstructionPhaseQueryDto.setCreateTime(DateUtils.getNowDate());
        return tbConstructionPhaseMapper.insertTbConstructionPhase(super.convertT(tbConstructionPhaseQueryDto));
    }

    /**
     * 修改施工阶段
     *
     * @param constructionId 施工阶段主键
     * @param tbConstructionPhaseQueryDto 施工阶段编辑实体
     * @return 结果
     */
    @Override
    public int updateTbConstructionPhase(Long constructionId, TbConstructionPhaseEditDto tbConstructionPhaseQueryDto) {
        tbConstructionPhaseQueryDto.setUpdateBy(getUserNameOrNotLogged());
        tbConstructionPhaseQueryDto.setUpdateTime(DateUtils.getNowDate());
        return tbConstructionPhaseMapper.updateTbConstructionPhase(super.convertT(tbConstructionPhaseQueryDto).setConstructionId(constructionId));
    }

    /**
     * 批量删除施工阶段
     *
     * @param constructionIds 需要删除的施工阶段主键集合
     * @return 结果
     */
    @Override
    public int deleteTbConstructionPhaseByConstructionIds(List<Long> constructionIds) {
        return tbConstructionPhaseMapper.deleteTbConstructionPhaseByConstructionIds(constructionIds);
    }

    /**
     * 删除单个施工阶段信息
     *
     * @param constructionId 施工阶段主键
     * @return 结果
     */
    @Override
    public int deleteTbConstructionPhaseByConstructionId(Long constructionId) {
        return tbConstructionPhaseMapper.deleteTbConstructionPhaseByConstructionId(constructionId);
    }

    @Override
    public TbEquipmentPhaseVo getPhase(TbEquipmentPhaseDto tbEquipmentPhaseDto) {
        return convertBean(tbConstructionPhaseMapper.selectOne(
                new LambdaQueryWrapper<TbConstructionPhaseDo>().eq(TbConstructionPhaseDo::getEngineeringId, tbEquipmentPhaseDto.getEngineeringId()).eq(TbConstructionPhaseDo::getProjectId, tbEquipmentPhaseDto.getProjectId())),
                TbEquipmentPhaseVo.class);
    }

    @Override
    public int savePhase(TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        Long l = tbConstructionPhaseMapper.selectCount(new LambdaQueryWrapper<TbConstructionPhaseDo>().eq(TbConstructionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()));
        if (l > 0) {
            tbConstructionPhaseMapper.update(convertT(tbEquipmentPhaseVo), new LambdaUpdateWrapper<TbConstructionPhaseDo>().eq(TbConstructionPhaseDo::getEngineeringId, tbEquipmentPhaseVo.getEngineeringId()).eq(TbConstructionPhaseDo::getProjectId, tbEquipmentPhaseVo.getProjectId()));
        } else {
            tbConstructionPhaseMapper.insert(convertT(tbEquipmentPhaseVo));
        }
        return 1;
    }

    @Override
    public int updatePhase(Long id, TbEquipmentPhaseVo tbEquipmentPhaseVo) {
        return tbConstructionPhaseMapper.updateById(convertT(tbEquipmentPhaseVo).setConstructionId(id));
    }
}
